package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.BusinessDistrict;
import com.turing.post.organization.util.BusinessDistrictMerchant;
import com.turing.post.organization.util.BusinessDistrictMerchantDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 商圈绑定商户业务实现（BusinessDistrictMerchant）
 * 
 * @author zxp
 * @Jan 3, 2013
 */
@Stateless(mappedName = "organization.impl.BusinessDistrictMerchantDaoImpl")
@Remote
@Local
public class BusinessDistrictMerchantDaoImpl implements
		BusinessDistrictMerchantDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public BusinessDistrictMerchant getBusinessDistrictMerchantByPrimaryKey(
			BusinessDistrictMerchant businessDistrictMerchant) {
		return _em.find(BusinessDistrictMerchant.class,
				businessDistrictMerchant);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessDistrictMerchant> getBusinessDistrictMerchantList(
			PCPSWebInput webInput, Integer page) {
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select b.* from BusinessDistrictMerchant b where b.merchantno like '"
					+ issueCompanyNo + "%' order by businessDistrictId";
		} else {
			sql = "select b.* from BusinessDistrictMerchant b order by businessDistrictId";
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em
				.createNativeQuery(sql, BusinessDistrictMerchant.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getBusinessDistrictMerchantTotal(PCPSWebInput webInput) {
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select nvl(count(*),0) from BusinessDistrictMerchant t where t.merchantno like '"
					+ issueCompanyNo + "%'";
		} else {
			sql = "select nvl(count(*),0) from BusinessDistrictMerchant t";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertBusinessDistrictMerchant(
			BusinessDistrictMerchant businessDistrictMerchant)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, businessDistrictMerchant);
	}

	@Override
	public void updateBusinessDistrictMerchant(
			BusinessDistrictMerchant businessDistrictMerchant)
			throws PCPSApplicationException {
		BusinessDistrictMerchant bdm = getBusinessDistrictMerchantByPrimaryKey(businessDistrictMerchant);
		if (bdm == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待修改商圈绑定商户信息为空!");
		}
		bdm.setMerchantNo(businessDistrictMerchant.getMerchantNo());
		GeneralDao.update(_em, bdm);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessDistrictMerchant> getBusinessDistrictMerchantListByMerchantNo(
			String merchantNo) {
		String sql = "select * from BusinessDistrictMerchant where merchantno =?1 order by businessDistrictId";
		Query query = _em
				.createNativeQuery(sql, BusinessDistrictMerchant.class);
		query.setParameter(1, merchantNo);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessDistrictMerchant> getBusinessDistrictMerchantListByBusinessDistrictId(
			BigInteger businessDistrictId) {
		String sql = "select * from BusinessDistrictMerchant where BusinessDistrictId =?1 order by MerchantNo";
		Query query = _em
				.createNativeQuery(sql, BusinessDistrictMerchant.class);
		query.setParameter(1, businessDistrictId);
		return query.getResultList();
	}

	@Override
	public void insertBusinessDistrictMerchantByList(BigInteger businessDistrictId,
			List<BusinessDistrictMerchant> bdmList)
			throws PCPSApplicationException {
		deleteBusinessDistrictMerchantByBusinessDistrictId(businessDistrictId);
		// 删除已绑定数据
		if (bdmList != null && bdmList.size() > 0) {
			BusinessDistrictMerchant bdm = null;
			for (BusinessDistrictMerchant b : bdmList) {
				bdm = new BusinessDistrictMerchant();
				bdm.setMerchantNo(b.getMerchantNo());
				bdm.setBusinessDistrictId(b.getBusinessDistrictId());
				insertBusinessDistrictMerchant(bdm);
			}
		}
	}

	@Override
	public void insertBusinessDistrictMerchantByList(String merchantNo,
			BigInteger[] arrInt) throws PCPSApplicationException {
		// 删除已绑定数据
		deleteBusinessDistrictMerchantByMerchantNo(merchantNo);
		BusinessDistrictMerchant bdm = null;
		for (BigInteger b : arrInt) {
			bdm = new BusinessDistrictMerchant();
			bdm.setMerchantNo(merchantNo);
			bdm.setBusinessDistrictId(b);
			insertBusinessDistrictMerchant(bdm);
		}
	}

	@Override
	public void deleteBusinessDistrictMerchantByMerchantNo(String merchantNo) {
		String sql = "delete from BusinessDistrictMerchant where merchantno =?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, merchantNo);
		query.executeUpdate();
	}

	private void deleteBusinessDistrictMerchantByBusinessDistrictId(
			BigInteger businessDistrictId) {
		String sql = "delete from BusinessDistrictMerchant where BusinessDistrictId =?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessDistrictId);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessDistrict> getBusinessDistrictByBranchNO(String branchNo) {
		String sql = "select * from BusinessDistrict b where b.BusinessDistrictId in (select t.BusinessDistrictId from BusinessDistrictMerchant t where t.MerchantNo = ?1)";
		Query query = _em.createNativeQuery(sql, BusinessDistrict.class);
		query.setParameter(1, branchNo);
		return query.getResultList();
	}
}
